package com.zdd.chap4a.sql;

import com.zdd.chap4a.entity.Book;
import org.apache.ibatis.jdbc.SQL;

public class SqlContext {
    //构造动态查询图书的sql语句
    public String searchBookSql(Book book){
        System.out.println(111);
        return new SQL(){
            {SELECT("*");
                FROM("book");
                if(book.getName()!=null&&book.getName()!=""){
                    WHERE(" name like '%' #{name} '%'");
                }
                if(book.getCategory()!=null&&book.getCategory()!=""){
                    WHERE(" category =#{category}");
                }
                if(book.getAuthor()!=null&&book.getAuthor()!=""){
                    WHERE(" author = #{author}");
                }
            }
        }.toString();
    }
}
